<?php

class edit_partner
{
	var $mess;

	function edit_partner()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if(isset($_POST['editItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select p_title from ". PARTNERS_TABLE . " Where p_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". PARTNERS_TABLE . " Where p_id = " . $_POST['itemID'];
			$result = $db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['p_title'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". PARTNERS_TABLE . " (p_enabled, p_title, p_web, p_description) values(" . 
					$_POST['enabled'] . ", '" . $_POST['title'] . "', '" . $_POST['web'] . "', '" . $_POST['description'] . "')" ;

			$result = $db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', $_POST['title'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". PARTNERS_TABLE . " set p_enabled = " . $_POST['enabled'] .
											     ", p_title = '" . $_POST['title'] .
												 "', p_web = '" . $_POST['web'] .
												 "', p_description = '" . $_POST['description'] .
												 "'  WHERE p_id = " . $_POST['id'] ;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['title'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
		}
		
		
		return $valid;
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select * From ". PARTNERS_TABLE . " Where p_id = " . $_POST['itemID'];
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
			$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
			$mess->render();
			
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
			$row['p_description'] = stripslashes($row['p_description']);
			
			$enabledOptions = '<option value="0" '. ($row['p_enabled']==0 ? 'SELECTED' : '').'>No</option>' .
							  '<option value="1" '. ($row['p_enabled']==1 ? 'SELECTED' : '').'>Yes</option>';
					
			include $skin_dir . 'admin/editpartner.tpl';
		}
		else
		{
			$mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			$mess->render();
		}
	}

	function renderAdd()
	{
		global $skin_dir,  $lang, $db;
		
		$mess = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$mess->render();
		
		$row['p_id'] = '';
		$row['p_title'] = '';
		$row['p_description'] = '';
		$row['p_web'] = '';
		$row['p_image'] = '';
		
		
		$enabledOptions = '<option value="0">No</option>' .
						  '<option value="1">Yes</option>';
		
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
		
		include $skin_dir . 'admin/editpartner.tpl';
	}
	
}
?>